﻿using System.Threading;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Helpers;
using Pharmacy;
using Pharmacy.Forms;
using VCS.Windows.Forms.Form.Main;

namespace Main.Forms
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MiniSkin : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private MiniFormUsers formUsers;
        private MiniFormPrescription formPrescription;
        private MiniFormAreaSettings formAreaSettings;
        private FormVoid formVoid;
        private FormHistory formHistory;
        private FormStock formStock;
        private Loading sp;
        private FormPatient formpatientEdit;
        private FormPatientActivation formActivation;
        private FormCompany formCompany;
        /// <summary>
        /// 
        /// </summary>
        public MiniSkin()
        {
            InitializeComponent();
            var th = new Thread(DoSplash);
            th.SetApartmentState(ApartmentState.STA);
            th.IsBackground = false;
            th.Start();
            //Thread.Sleep(7000);
            Thread.Sleep(1000);
            th.Abort();
            //th.Join();
            LoadByRoles();
            SkinHelper.InitSkinGallery(rddAppearance, true);
            Text = string.Format("{0} Version {1}", GetType().Assembly.GetName().Name, GetType().Assembly.GetName().Version);
        }

        public override sealed string Text
        {
            get { return base.Text; }
            set { base.Text = value; }
        }


        private void DoSplash()
        {
            sp = new Loading {TopMost = true};
            sp.ShowDialog();
            //sp.BeginInvoke(new MethodInvoker(CloseSphash));
        }

        //private void CloseSphash()
        //{
        //    Thread.Sleep(7000);
        //    sp.Close();
        //}

        private void LoadByRoles()
        {
            //bPrescription.Enabled = ServiceFacade.SettingsHelper.AllowPrescription;
            //formPrescription = new MiniFormPrescription { MdiParent = this };
            //formPrescription.Show();

            string roles = Program.GlobalUser.Roles;
            switch (roles)
            {
                case "Pharmacist":
                    formPrescription = new MiniFormPrescription { MdiParent = this };
                    formPrescription.Show();
                    break;
                case "Account":
                    break;
                case "Super-Cashier":
                    break;
                case "Admin":
                    pSettings.Visible = true;
                    break;
            }
            //Test Print Label
            //FormPrescriptionLabel formPrescriptionLabel = new FormPrescriptionLabel();
            //formPrescriptionLabel.LoadDefault("DN-HCM-01-111101004");
            //formPrescriptionLabel.Show();
        }

        private void Users_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (formUsers == null || formUsers.IsDisposed)
            {
                formUsers = new MiniFormUsers { MdiParent = this };
                formUsers.Show();
            }
            else
            {
                formUsers.BringToFront();
            }
        }

        private void Prescription_ItemClick(object sender, ItemClickEventArgs e)
        {
            var formPre = new MiniFormPrescription { MdiParent = this };
            formPre.Show();
        }

        private void bAreaSettings_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (formAreaSettings == null || formAreaSettings.IsDisposed)
            {
                formAreaSettings = new MiniFormAreaSettings { MdiParent = this };
                formAreaSettings.Show();
            }
            else
            {
                formAreaSettings.BringToFront();
            }
        }

        private void bVoid_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (formVoid == null || formVoid.IsDisposed)
            {
                formVoid = new FormVoid { MdiParent = this };
                formVoid.Show();
            }
            else
            {
                formVoid.BringToFront();
            }
        }

        private void bHistory_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (formHistory == null || formHistory.IsDisposed)
            {
                formHistory = new FormHistory { MdiParent = this };
                formHistory.Show();
            }
            else
            {
                formHistory.BringToFront();
            }
        }

        private void bStock_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (formStock == null || formStock.IsDisposed)
            {
                formStock = new FormStock { MdiParent = this };
                formStock.Show();
            }
            else
            {
                formStock.BringToFront();
            }
        }

        private void bPatient_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (formpatientEdit == null || formpatientEdit.IsDisposed)
            {
                formpatientEdit = new FormPatient { MdiParent = this };
                formpatientEdit.Show();
            }
            else
            {
                formpatientEdit.BringToFront();
            }
        }

        private void bActivation_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (formActivation == null || formActivation.IsDisposed)
            {
                formActivation = new FormPatientActivation { MdiParent = this };
                formActivation.Show();
            }
            else
            {
                formActivation.BringToFront();
            }
        }

        private void bCompany_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (formCompany == null || formCompany.IsDisposed)
            {
                formCompany = new FormCompany { MdiParent = this };
                formCompany.Show();
            }
            else
            {
                formCompany.BringToFront();
            }
        }

        private void bInvoice_ItemClick(object sender, ItemClickEventArgs e)
        {
            var formInvoice = new FormInvoice {MdiParent = this};
            formInvoice.Show();
        }
    }
}